package com.md.base;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import com.md.nohttp.CallServer;
import com.md.nohttp.CustomHttpListener;
import com.md.nohttp.CustomHttpListenermoney;
import com.md.utils.CommonUtil;
import com.md.utils.PreferencesUtils;
import com.yolanda.nohttp.rest.Request;
import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;


/**
 * 作者 亢兰兰
 * 时间 2018/5/11 0011
 * 公司  郑州软盟
 */

public class BaseFragment extends Fragment {
    /**
     * RecyclerView数据管理的LayoutManager
     */
    public LinearLayoutManager linearLayoutManager;
    public GridLayoutManager gridLayoutManager;
    public StaggeredGridLayoutManager staggeredGridLayoutManager;
    /**
     * SlimAdapter的adapter
     */
    public SlimAdapter mAdapter;
    public SlimAdapterEx mAdapterex;
    /**
     * 是否正在上拉加载中
     */
    public boolean isLoadingMore;
    public int pager = 1;
    /**
     * 网络请求对象.
     */
    public Request<String> mRequest;
    //页面跳转
    public void StartActivity(Class c) {
        Intent intent = new Intent(getActivity(), c);
        this.startActivity(intent);
    }

    public void StartActivity(Class c,int tag) {
        Intent intent = new Intent(getActivity(), c);
        intent.putExtra("tag",tag);
        this.startActivity(intent);
    }

    public void call(Context context, String phone) {
        if (TextUtils.isEmpty(phone)) {
            CommonUtil.showToast(context, "无效电话号码");
            return;
        }
        Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));//跳转到拨号界面，同时传递电话号码
        startActivity(dialIntent);
    }
    //显示吐丝
    public void showtoa(String string) {
        Toast.makeText(getActivity(),string,Toast.LENGTH_SHORT).show();
    }

    //Nohttp请求
    public  <T> void getRequest(CustomHttpListener<T> customHttpListener, boolean isloading) {
        if (!TextUtils.isEmpty(PreferencesUtils.getString(getActivity(), "token"))) {
            mRequest.addHeader("token", PreferencesUtils.getString(getActivity(), "token"));
        }
        mRequest.add("environment", PreferencesUtils.getInt(getActivity(), "languare"));
        CallServer.getRequestInstance().add(getActivity(), mRequest, customHttpListener, isloading);
    }

    public  <T> void getRequest(CustomHttpListenermoney<T> customHttpListener, boolean isloading) {
        if (!TextUtils.isEmpty(PreferencesUtils.getString(getActivity(), "token"))) {
            mRequest.addHeader("token", PreferencesUtils.getString(getActivity(), "token"));
        }
        mRequest.add("environment",PreferencesUtils.getInt(getActivity(), "languare"));
        CallServer.getRequestInstance().add(getActivity(), mRequest, customHttpListener, isloading);
    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        // 退出时可以取消这个请求
        if (mRequest != null)
            mRequest.cancel();
    }








}
